package com.app.common

import android.app.Application
import android.content.Context
import android.media.MediaScannerConnection
import android.os.Environment
import java.io.File

object MediaStorage {
    const val DIRECTORY_TYPE_PLAYBACK = 11
    const val DIRECTORY_TYPE_SNAP = 22
    const val DIRECTORY_TYPE_EMERGENCY = 33

    const val directoryName = "MyCamera"
    private const val playbackDirectoryName = "norm"
    private const val snapDirectoryName = "photo"
    private const val emergencyDirectoryName = "emr"
    private lateinit var appContext: Context
    private var directory: File? = null
    private var playbackDirectory: File? = null
    private var snapDirectory: File? = null
    private var emergencyDirectory: File? = null
    fun init(application: Application) {
        appContext = application
    }

    fun getMediaDirectory(): File {
        if (directoryName.isEmpty()) {
            throw IllegalStateException("mediaDirectoryName is empty")
        }
        if (directory == null) {
            val dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
            val dir = File(dcim, directoryName)
            if (!dir.exists()) {
                dir.mkdirs()
            }
            directory = dir
        }
        return directory!!
    }

    fun getMediaDirectory(type: Int): File {
        if (directoryName.isEmpty()) {
            throw IllegalStateException("mediaDirectoryName is empty")
        }
        return when (type) {
            DIRECTORY_TYPE_PLAYBACK -> {
                if (playbackDirectory == null) {
                    val dcim =
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                    val dir = File(dcim, directoryName)
                    if (!dir.exists()) {
                        dir.mkdirs()
                    }
                    val dir1 = File(dir, playbackDirectoryName)
                    if (!dir1.exists()) {
                        dir1.mkdirs()
                    }
                    playbackDirectory = dir1
                }
                playbackDirectory!!
            }
            DIRECTORY_TYPE_SNAP -> {
                if (snapDirectory == null) {
                    val dcim =
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                    val dir = File(dcim, directoryName)
                    if (!dir.exists()) {
                        dir.mkdirs()
                    }
                    val dir1 = File(dir, snapDirectoryName)
                    if (!dir1.exists()) {
                        dir1.mkdirs()
                    }
                    snapDirectory = dir1
                }
                snapDirectory!!
            }
            DIRECTORY_TYPE_EMERGENCY -> {
                if (emergencyDirectory == null) {
                    val dcim =
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                    val dir = File(dcim, directoryName)
                    if (!dir.exists()) {
                        dir.mkdirs()
                    }
                    val dir1 = File(dir, emergencyDirectoryName)
                    if (!dir1.exists()) {
                        dir1.mkdirs()
                    }
                    emergencyDirectory = dir1
                }
                emergencyDirectory!!
            }
            else -> playbackDirectory!!
        }
    }

    fun deleteMediaFile(paths: Array<String>) {
        paths.forEach { File(it).delete() }
        scan(paths)
    }

    fun deleteMediaFile(paths: List<String>) {
        paths.forEach { File(it).delete() }
        scan(paths.toTypedArray())
    }

    fun scan(files: Array<String>) {
        MediaScannerConnection.scanFile(appContext, files, null, null)
    }

    fun getPath(name: String): String {
        if (directory == null) {
            getMediaDirectory()
        }
        return File(directory, name).absolutePath
    }

    fun getFileByDirectory(name: String): File {
        if (directory == null) {
            getMediaDirectory()
        }
        return File(directory, name)
    }

    fun getFileByDCIMDirectory(name: String): File {
        val directoryDCIM=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
        return File(directoryDCIM, name)
    }

    fun getPath(type: Int, name: String): String {
        return when (type) {
            DIRECTORY_TYPE_PLAYBACK -> {
                if (playbackDirectory == null) {
                    getMediaDirectory(type)
                }
                File(playbackDirectory, name).absolutePath
            }
            DIRECTORY_TYPE_SNAP -> {
                if (snapDirectory == null) {
                    getMediaDirectory(type)
                }
                File(snapDirectory, name).absolutePath
            }
            DIRECTORY_TYPE_EMERGENCY -> {
                if (emergencyDirectory == null) {
                    getMediaDirectory(type)
                }
                File(emergencyDirectory, name).absolutePath
            }
            else -> ""
        }
    }

    fun exists(name: String): Boolean {
        if (directory == null) {
            getMediaDirectory()
        }
        return directory?.let {
            File(it, getFileName(name)).exists()
        } ?: false
    }

    fun exists(type: Int, name: String): Boolean {
        return when (type) {
            DIRECTORY_TYPE_PLAYBACK -> {
                if (playbackDirectory == null) {
                    getMediaDirectory(type)
                }
                playbackDirectory?.let {
                    File(it, getFileName(name)).exists()
                } ?: false
            }
            DIRECTORY_TYPE_SNAP -> {
                if (snapDirectory == null) {
                    getMediaDirectory(type)
                }
                snapDirectory?.let {
                    File(it, getFileName(name)).exists()
                } ?: false
            }
            DIRECTORY_TYPE_EMERGENCY -> {
                if (emergencyDirectory == null) {
                    getMediaDirectory(type)
                }
                emergencyDirectory?.let {
                    File(it, getFileName(name)).exists()
                } ?: false
            }
            else -> false
        }
    }

    fun getFileName(path: String): String {
        val index = path.lastIndexOf(File.separatorChar)
        if (index == -1) {
            return path
        }
        return path.substring(index + 1)
    }
}